#include "WMI_Share.h"
using namespace std;
using namespace log4cxx;
using namespace log4cxx::xml;
using namespace log4cxx::helpers;

WMI_Share::WMI_Share(IWbemClassObject* obj)
{
	DOMConfigurator::configure("LogConfig.xml");
    VARIANT var;
    HRESULT hr;

    for(int i = 0; i <= 9; i++)
    {
		hr = obj->Get(properties[i].c_str(), 0, &var, 0, 0);
        if(SUCCEEDED(hr))
        {
            setData(var, i);
        }
    }

    VariantClear(&var);
}

WMI_Share::~WMI_Share()
{
}

const wstring WMI_Share::properties[] = {WACCESS_MASK, ALLOW_MAXIMUN, CAPTION, DESCRIPTION, 
									INSTALL_DATE, MAXIMUN_ALLOWED, NAME, PATH, STATUS, TYPE};

void WMI_Share::setData(VARIANT& var, int i)
{
    switch(i)
    {
    case 0:
        setAccessMask(var.lVal);
        break;
    case 1:
        setAllowMaximum(var.boolVal);
        break;
    case 2:
        setCaption(wstring(var.bstrVal));
        break;
    case 3:
        setDescription(wstring(var.bstrVal));
        break;
    case 4:
        if(V_VT(&var) != VT_NULL)
        {
            setInstallDate(WMI_Utils::wcharToCTime(var.bstrVal));
        }
        break;
    case 5:
        setMaximumAllowed(var.lVal);
        break;
    case 6:
        setName(wstring(var.bstrVal));
        break;
    case 7:
        setPath(wstring(var.bstrVal));
        break;
    case 8:
        setStatus(wstring(var.bstrVal));
        break;
    case 9: 
        setType(var.intVal);
        break;
    }
}

void WMI_Share::print()
{
	wstringstream wout;
	LoggerPtr loggerWShare(Logger::getLogger("loggerWShare"));
	
	string date=WMI_Utils::formatStd(getInstallDate());
	wstring wdate(date.begin(),date.end());

    wout << "Path: " << getPath() << std::endl;
    wout << "Name: " << getName() << std::endl;
    wout << "Caption: " << getCaption() << std::endl;
    wout << "Description: " << getDescription() << std::endl;
    wout << "Install Date: " << wdate << std::endl;
    wout << "Allows Maximum: " << getAllowMaximum() << std::endl;
    wout << "Maximum Allowed: " << getMaximumAllowed() << std::endl;
    wout << "Status: " << getStatus() << std::endl;
    wout << "Type: " << getType() << std::endl;

	LOG4CXX_INFO(loggerWShare, wout.str());
}

